<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/12
 * Time: 15:47
 */

namespace App\HttpController;


use EasySwoole\Config;
use EasySwoole\Core\Component\Di;
use EasySwoole\Core\Http\AbstractInterface\Controller;
use EasySwoole\Core\Http\Request;
use EasySwoole\Core\Http\Response;
use EasySwoole\Core\Component\Cache\Cache;

class AbstractBase extends Controller
{
    protected $start_time;
    protected $end_time;
    protected $member_info;
    protected $coin_type = [
        '1' => 10,
        '2' => 50,
        '3' => 100,
        '4' => 500,
        '5' => 1000
    ];
    protected $bet_unit = [
        'wan' => '万',
        'qian' => '千',
        'bai' => '百',
        'shi' => '十',
        'ge' => '个'
    ];
    protected $settings = [];

    protected $db;


    public function __construct(string $actionName, Request $request, Response $response)
    {
        $this->db = Di::getInstance()->get("MYSQL");
        $dbconf = Config::getInstance()->getConf('database');
        $this->db->setPrefix($dbconf['prefix']);
        $rate = [];
        $where = ['da_rate', 'xiao_rate', 'dan_rate', 'shuang_rate', 'two_star_rate'];
        $this->db->where('name', ['in' => $where]);
        $res = $this->db->get('settings');

//        $res = Db::name('settings')->field('name,value')->where($where)->select();
        if (!empty($res)) {
            foreach ($res as $item => $v) {
                $rate[$v['name']] = $v['value'];
            }
        }
        $this->settings = $rate;
        unset($res);
        $this->start_time = strtotime("10:00");
        $this->end_time = strtotime("01:55");
        parent::__construct($actionName, $request, $response);
    }

    public function index()
    {
        return false;
    }


    protected function success($data = null, $msg = '')
    {
        if (!$this->response()->isEndResponse()) {
            $data = Array(
                "ResponseStatus" => 0,
                "datas" => $data,
                "ResponseMsg" => $msg,

            );
            $this->response()->write(json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
            $this->response()->withHeader('Content-type', 'application/json;charset=utf-8');
            return true;
        } else {
            trigger_error("response has end");
            return false;
        }
    }

    protected function error($data = null, $msg)
    {
        if (!$this->response()->isEndResponse()) {
            $data = Array(
                "ResponseStatus" => 100,
                "datas" => $data,
                "ResponseMsg" => $msg,

            );
            $this->response()->write(json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
            $this->response()->withHeader('Content-type', 'application/json;charset=utf-8');
            return true;
        } else {
            trigger_error("response has end");
            return false;
        }
    }

    protected function getCurrentRoundInfo()
    {
        $current_time = time();
        $round_cache = Cache::getInstance()->get('round_cache');
        $result = $round_cache['round_list'];
        $current_round_id = 0;
        $round_curr_info = [];
        if (!empty($result)) {
            foreach ($result as $item => $value) {
                if ($current_time >= $value['start_time'] && $current_time < $value['stop_time']) {
                    $current_round_id = $value['round_id'];
                    $round_curr_info = $value;
                    break;
                }
            }

            return ['round_id' => $current_round_id, 'round_info' => $round_curr_info,'round_period' => $round_cache['game_period']];

        }

        return false;
    }


    protected function onRequest($action): ?bool
    {

        $login_token = $this->request()->getRequestParam('token');
        if (!$login_token) {
            $this->error([], 'Token未传入');
            return false;
        }
        if (!empty($login_token)) {
            $this->db->where('token', $login_token);
            $res = $this->db->getOne('mb_user_token');
//            $res = Db::name('MbUserToken')->where(['token' => $login_token])->find();
            if (empty($res)) {
                $this->error([], '用户不存在或登录超时');
                return false;
            }
            $this->member_info = $res;
        } else {
            $this->error([], '请先登录');
            return false;
        }

        return parent::onRequest($action); // TODO: Change the autogenerated stub
    }


}